package com.xy.novelmanagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xy.model.Novel;
import com.xy.novelmanagement.mapper.AutoPlayManagementMapper;
import com.xy.novelmanagement.service.IAutoPlayManagementService;
import com.xy.utils.JedisUtil;
import com.xy.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AutoPlayManagementServiceImpl implements IAutoPlayManagementService {
		@Autowired
		private AutoPlayManagementMapper autoPlayManagementMapper;
		private final ObjectMapper objectMapper = new ObjectMapper();
		@Override
		public List<Novel> getAutoPlayList() {
				Jedis jedis = JedisUtil.getConnect();
				List<Novel> list = new ArrayList<>();

				try {
						for (int i = 0; i < 4; i++) {
								String key = "Colums" + (i + 1);
								String values = jedis.get(key);
								log.info("AutoPlayManagementServiceImpl getAutoPlayList values : " + values);
								Novel novel = JSON.parseObject(values, Novel.class);

								if (novel != null) {
										list.add(novel);
								} else {
										list.add(new Novel());
								}
						}
				} catch (Exception e) {
						e.printStackTrace();
				}finally {
						JedisUtil.close(jedis);
				}
				log.info("AutoPlayManagementServiceImpl getAutoPlayList list : " + list);
				return list;
		}

		@Override
		public Integer setAllAutoPlay(String[] tips, List<Novel> replaceNames) {
				Integer code = -403;
				Jedis jedis = JedisUtil.getConnect();
				try {
						for (int i = 0; i < 4; i++) {
								if (!tips[i].equals("0")) {
										String key = "Colums" + tips[i];
										Novel novel = autoPlayManagementMapper.getNovelByName(replaceNames.get(i).getBookName());
										if (jedis.exists(key)) {
												jedis.set(key, objectMapper.writeValueAsString(novel));
										} else {
												jedis.setnx(key, objectMapper.writeValueAsString(novel));
										}
										int hour = TimeUtil.getHour();
										hour = 24 - hour;
										jedis.expire(key, 60 * 60 * hour);
								}
						}
						code = 200;
				} catch (Exception e) {
						log.error("setAllAutoPlay 错误消息：{}", e.getMessage(), e);
						return 400;
				}finally {
						JedisUtil.close(jedis);
				}

				return code;
		}

		@Override
		public Integer setOneAutoPlay(String location, Novel novel) {
				Integer code = -403;
				Jedis jedis = JedisUtil.getConnect();
				try {
						novel = autoPlayManagementMapper.getNovelByName(novel.getBookName());
						if (!location.equals("0")) {
								String key = "Colums" + location;
								jedis.set(key, objectMapper.writeValueAsString(novel));
								int hour = TimeUtil.getHour();
								hour = 24 - hour;
								jedis.expire(key, 60 * 60 * hour);
								code = 200;
						}

				} catch (Exception e) {
						log.error("setAllAutoPlay 错误消息：{}", e.getMessage(), e);
						return 400;
				}finally {
						JedisUtil.close(jedis);
				}
				return code;
		}

}
